package warehouse;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;


import javax.swing.JButton;
import javax.swing.JFrame;


import javax.swing.JTextArea;



public class Gui extends JFrame  implements ActionListener, Observer {

private static final long serialVersionUID = 1L;
private static OrderList allOrders;
private ItemList allItems;
public Thread thread1,thread2;

JButton start, stop ;
static JTextArea displayList1;
static JTextArea displayList2;
static JTextArea displayOrders;
JTextArea displayItems;
	
public Gui (OrderList orders, ItemList items,Item ms){
	setTitle("Warehouse");
	allOrders = orders;
	allItems = items;
	
	ms.registerObserver(this);
	setDefaultCloseOperation(javax.swing.JFrame.DO_NOTHING_ON_CLOSE);
	setupEastPanel(); 
	setupNorthPanel();
	setupCenterPanel();
	setupWestPanel();
	
	
		
	pack();
	setVisible(true);
	
	
		
}


//implementation of Center Panel
private void setupCenterPanel(){
	//components// displayList,scrollList
	JPanel center=new JPanel();
	center.setLayout(new GridLayout(2,1));
	displayList1 = new JTextArea(4,30);
    displayList1.setFont(new Font (Font.MONOSPACED, Font.PLAIN,11));
    displayList1.setEditable(false);
    
    displayList2 = new JTextArea(4,30);
    displayList2.setFont(new Font (Font.MONOSPACED, Font.PLAIN,11));
    displayList2.setEditable(false);
    displayList1.setText("WORKER 1");
    displayList2.setText("WORKER 2");
    center.add(displayList1);
    center.add(displayList2);
    this.add(center,BorderLayout.CENTER);
	
}
//implementation of East Panel
private void setupEastPanel(){

	JPanel east = new JPanel();
	displayOrders= new JTextArea(15,30);
	displayOrders.setFont(new Font (Font.MONOSPACED, Font.PLAIN,11));
	displayOrders.setEditable(false);
	String orders="LIST OF ORDERS TO PROCESS\n";
	orders+= allOrders.listDetails();
	
	displayOrders.setText(orders);
	east.add(displayOrders);
	
	
	this.add(east,BorderLayout.EAST);
	
	
	
}
//implementation of West Panel
private void setupWestPanel(){

	JPanel west = new JPanel();
	displayItems= new JTextArea(15,30);
	displayItems.setFont(new Font (Font.MONOSPACED, Font.PLAIN,11));
	displayItems.setEditable(false);
	
	String items="WAREHOUSE ITEMS\n";
	items+= allItems.printItems();
	
	displayItems.setText(items);
	
	west.add(displayItems);
	
	this.add(west,BorderLayout.WEST);
	
}
//implementation of North Panel
private void setupNorthPanel(){
	//components: comNum, compName
	JPanel north = new JPanel();
	start = new JButton("START DAY");
	start.addActionListener(this);
	stop = new JButton("END DAY");
	stop.addActionListener(this);
	north.add(start);
	north.add(stop);
	this.add(north,BorderLayout.NORTH);
	
}

//define what the buttons do for us
public void actionPerformed(ActionEvent e) {
	if (e.getSource() == start){
		start.setEnabled(false);
		process();
	}
		
	if (e.getSource() == stop){
		start.setEnabled(true);
		endDay();
}
}
public void process() {
	
	thread1= new Thread( new Worker(allOrders,allItems,1));
	thread1.start();
	thread2= new Thread( new Worker(allOrders,allItems,2));
	thread2.start();
	
	
}

public void update(){
	displayItems.setText("WAREHOUSE ITEMS\n"+allItems.printItems());
	
}

public synchronized static void setDisplay(String order, int w){
	if (w==1){
displayList1.setText("WORKER 1\n"+order);}
	if (w==2){
displayList2.setText("WORKER 2\n"+order);}
	displayOrders.setText("LIST OF ORDERS TO PROCESS\n"+allOrders.listDetails());
}

public void endDay(){
	thread1.interrupt();
	thread2.interrupt();
	
}

}



